// 对axios进行二次封装

import Axios from "axios";
export default function requestApi(url = "", params = {}, type = "get") {

    let promise;
    return new Promise((resolve, reject) => {
        //判断请求数据的方式---是get--还是post
        if (type.toLowerCase() == "get") {
            let paramsStr = "";
            Object.keys(params).forEach(key => {
                paramsStr += key + "=" + params[key] + "&"
            })
            if (paramsStr != "") {
                paramsStr = paramsStr.substr(0, paramsStr.lastIndexOf('&'))
            }
            // 拼接URL
            url += "?" + paramsStr
            promise = Axios.get(url)
        } else if (type.toLowerCase() == "post") {
            promise = Axios.post(url, params)
        }
        promise.then(response => {
            resolve(response.data)
        }).catch(err => {
            reject(err)
        })
    })
}